package org.hmh.offer;

import java.util.Stack;

/**
 * Created this one by HMH on 2018/1/20.
 */
public class t16 {
    static public class Solution {
        public boolean IsPopOrder(int[] pushA, int[] popA) {
            if (pushA.length == 0) {
                return true;
            }

            Stack<Integer> stack = new Stack<>();
            int count = 0;
            for (int i = 0; i < popA.length; i++) {
                int toPop = popA[i];
                if (stack.size() > 0 && toPop == stack.peek()) {
                    stack.pop();
                    continue;
                }
                while (count < pushA.length) {
                    if(toPop == pushA[count]){
                        count++;
                    }
                    stack.push(pushA[count++]);
                }
            }
            return true;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.IsPopOrder(new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3 ,2 ,1}));
    }

}
